import { Injectable } from '@angular/core';
import { FrameContext } from '@farris/devkit';
import { MessagerService } from '@farris/ui-messager';
import { BefRepository, ResponseInfo } from '@farris/bef';
import {  BeActionService, FormMessageService ,FormNotifyService,CommandService } from '@farris/command-services';
import { EMPTY, of } from 'rxjs';
import { tap,switchMap } from 'rxjs/operators'

@Injectable()
export class SbssSbtzFrmControllerService {
  constructor(
    private frameContext: FrameContext,
    private messageService: MessagerService,
    private commandService: CommandService,
    private beActionService: BeActionService,
  ) {}

   /*
  * 筛选方案默认值设置
  */
  setDefaultConfigs() {
    const defaultValue = {
      fieldConfigs: [
          {//下拉
              labelCode: "del",
              value: {
                  key: "false",
                  value: [
                    { value: "false",name: "启用" }
                  ]
              }
          },
      ]}

    this.frameContext.uiState['defaultConfigs'] = defaultValue
  }

  /**
   * 检查当前树是否是末级
   * @remarks 
   * @returns 
   */
  public checkTreeIsDetail(){
    let lbIsDetail = this.frameContext.uiState['lbIsDetail'];
    
    if(!lbIsDetail){
      this.messageService.warning("请选择明细12位设备类别!")
      return EMPTY
    }
  }


  /**
   * 手动刷新
   * @remarks 
   * @returns 
   */
  public filterExecute(){
    this.commandService.execute("Filter1","root-component");
  }

  /**
   * 开启
   */
  activeByComm(): any {
    let rowData = this['context'].eventParam.rowData;
    let field = this['context'].eventParam.field



    let id = rowData.id
    let active = rowData[field]

    if (field === 'del'){
      active = !active
    }

    const rep = this.frameContext.repository as BefRepository<any>;
    const repInfo = rep.restService.buildRequestInfo();

    const actionUri = 'chageactive';
    const body = {
      id: id,
      active:active,
      datacomm:field,
      requestInfo: repInfo
    }


    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
        let msg = ""
        if (field === 'del'){
         active = !active
        }
        if(active){
          msg = "已开启!"
        }else{
          msg = "已关闭!"
        }
        this.messageService.info(msg);
        this.commandService.execute("Filter1","root-component");
      }))  

  }

  /**
   * 单条设备关闭
   * @param id 设备ID
   * @param active 状态
   */
  closeDevice(id:string){

    const rep = this.frameContext.repository as BefRepository<any>;
    const repInfo = rep.restService.buildRequestInfo();

    const actionUri = 'chageactive';
    const body = {
      id: id,
      active:true,
      datacomm:"del",
      requestInfo: repInfo
    }

    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
        let msg = "设备已关闭!"
        this.messageService.info(msg);
        this.commandService.execute("Filter1","root-component");
      }))  
  }

    /**
   * 单条设备开启
   * @param id 设备ID
   * @param active 状态
   */
  openDevice(id:string){

    const rep = this.frameContext.repository as BefRepository<any>;
    const repInfo = rep.restService.buildRequestInfo();

    const actionUri = 'chageactive';
    const body = {
      id: id,
      active:false,
      datacomm:"del",
      requestInfo: repInfo
    }

    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
        let msg = "设备已开启!"
        this.messageService.info(msg);
        this.commandService.execute("Filter1","root-component");
      }))  
  }


  /**
   * 设备二维码信息刷新
   */
  reflashQR(ids){

    const rep = this.frameContext.repository as BefRepository<any>;
    const repInfo = rep.restService.buildRequestInfo();

    const actionUri = 'reflashqr';
    const body = {
      id: ids,
      requestInfo: repInfo
    }

    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => {
        let msg = "二维码信息已刷新!"
        this.messageService.info(msg);
        this.commandService.execute("Filter1","root-component");
      }))  
  }
}
